home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / Susp.sml < prev    next >
Encoding:
Text File  |  1996-07-03  |  412 b   |  18 lines  |  [TEXT/R*ch]

  1. (* Susp -- support for lazy evaluation 1995-05-22 *)
  2.  
  3. local 
  4.     datatype 'a thunk = VAL of 'a | THUNK of unit -> 'a
  5.     prim_val magic : 'a -> 'b = 1 "identity"
  6. in
  7.     type 'a susp = 'a thunk ref
  8.  
  9.     fun delay (f : unit -> 'a) = 
  10.     magic (ref (THUNK (magic f))) : 'a susp
  11.  
  12.     fun force (su : 'a susp) : 'a = 
  13.     case !su of
  14.         VAL v   => v 
  15.       | THUNK f => let val v = f () 
  16.                in su := VAL v; v end
  17. end
  18.